package com.whfc.openapi.api.face.controller;

import com.whfc.common.face.yunqi.EmpSyncParam;
import com.whfc.common.face.yunqi.FaceGateSyncParam;
import com.whfc.common.face.yunqi.RecSyncParam;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.emp.enums.FaceGateType;
import com.whfc.emp.param.EmpAttendSyncDataParam;
import com.whfc.emp.param.EmpDeviceSyncDataParam;
import com.whfc.emp.param.EmpInfoSyncDataParam;
import com.whfc.fuum.dto.open.OpenApiClientDTO;
import com.whfc.openapi.constant.OpenApiConst;
import com.whfc.openapi.api.face.service.FaceGateSyncService;
import com.whfc.openapi.api.face.util.FaceGateDataConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 云启测温-人脸同步
 *
 * @author : sunguodong
 * @version : 1.0
 * @date : 2020-09-23
 */
@RestController
@RequestMapping("/open/api/v1/yunqi/sync")
public class FaceGateYunQiController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private FaceGateSyncService faceGateSyncService;

    @PostMapping("/rec/record")
    public Result syncRecRecord(@Validated @RequestBody RecSyncParam body, @RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO) {
        logger.info("云启考勤记录同步,body:{}", body.toString());
        EmpAttendSyncDataParam dataParam = FaceGateDataConverter.attendDataConverter(body);
        if (dataParam != null) {
            dataParam.setDeptId(clientDTO.getDeptId());
            dataParam.setPlatform(FaceGateType.YUNQI.getCode());
            faceGateSyncService.syncAttend(dataParam);
        }
        return ResultUtil.success();
    }

    @PostMapping("/device")
    public Result syncDeviceList(@Validated @RequestBody FaceGateSyncParam body, @RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO) {
        logger.info("云启设备同步,body:{}", body.toString());
        EmpDeviceSyncDataParam dataParam = FaceGateDataConverter.deviceDataConverter(body);
        if (dataParam != null) {
            dataParam.setDeptId(clientDTO.getDeptId());
            dataParam.setPlatform(FaceGateType.YUNQI.getCode());
            faceGateSyncService.syncDevice(dataParam);
        }
        return ResultUtil.success();
    }

    @PostMapping("/emp")
    public Result syncEmpList(@Validated @RequestBody EmpSyncParam body, @RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO clientDTO) {
        logger.info("云启人员同步,body:{}", body.toString());
        EmpInfoSyncDataParam dataParam = FaceGateDataConverter.empInfoDataConverter(body);
        if (dataParam != null) {
            dataParam.setDeptId(clientDTO.getDeptId());
            dataParam.setPlatform(FaceGateType.YUNQI.getCode());
            faceGateSyncService.syncEmpInfo(dataParam);
        }
        return ResultUtil.success();
    }
}
